import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { AlertController } from '@ionic/angular';

@Component({
  selector: 'app-tab4',
  templateUrl: './tab4.page.html',
  styleUrls: ['./tab4.page.scss'],
})
export class Tab4Page implements OnInit {

  constructor(public http: HttpClient, public alertC: AlertController) { }
  name = '';
  pwd = '';
  ngOnInit() {
  }
  login() {
    let url = "http://101.96.128.94:9999/data/user/login.php";
    // POST要求路径和参数分开传递
    let params = `uname=${this.name}&upwd=${this.pwd}`;
    // 添加特殊的内容配置
    let options = {
      headers: new HttpHeaders({
        'content-type': 'application/x-www-from-urllencoded',
      }),
    };
    this.http.post(url, params, options).subscribe(
      (res: any) => {
        console.log(res);
        let code = res.code;
        if (code == 200) {
          this.alertC.create({
            header: '恭喜',
            message: '登录成功',
            buttons: ['确定'],
          }).then(res => res.present());
        } else {
          this.alertC.create({
            header: '很遗憾',
            message: '登陆失败',
            buttons: ['确定']
          }).then(res => res.prepend())
        }
      }
    )

  }
}
